﻿<%
'''分页类
Class PagerClass
	'分页GET参数名,跳转目标页,每页记录,当前页数,总页数,总记录数,
	Public PageVar, TargetPage, PageSize, PageNow, PageCount, Recordcount
	'分页方式GET/POST,链接格式化函数,分行显示分页列表,分行显示下拉列表,分页左偏移量,分页右偏移量,下拉框选项左/右修饰符,默认选中下拉框值, 分页风格
	Public Method, FormatUrlFun, LineList, LineSelect, LeftOffSet, RightOffSet, OptionLeft, OptionRight, OptionSelected, ListMode
	'第一页模型,最后页模型,上一页模型,下一页模型,当前页模型,其他页模型,跳转框模型,跳转按钮模型,下拉框模型,上一组模型,下一组模型
	Private s_first, s_last, s_prev, s_next, s_nmod, s_amod, s_jump, s_button, s_select, s_prevg, s_nextg
	'强制显示第一页/最后页/上一页/下一页, 跳转输入框ID
	Private s_forcedFirst, s_forcedLast, s_forcedPrev, s_forcedNext, s_inputId, s_forcedPrevG, s_forcedNextG
	'URL参数总字符串,链接模型,用于POST的隐藏域,链接分页识别符,分页模版
	Private s_paraStr, s_hrefModel, s_hiddenStr, s_identifier, s_model
	
	'''构造
	Private Sub Class_Initialize()
		PageVar = "p"
		TargetPage = "/"
		PageSize = 10
		PageNow = Null
		PageCount = 1
		Recordcount = 0
		Method = "GET"
		FormatUrlFun = ""
		LineList = True
		LineSelect = True
		LeftOffSet = 5
		RightOffSet = 5
		OptionLeft = ""
		OptionRight = ""
		OptionSelected = True
		ListMode = 3
		s_first = "<a href=""{url}"" class=""first"">{page}</a>-"
		s_last = "-<a href=""{url}"" class=""last"">{page}</a>"
		s_prev = "<a href=""{url}"" class=""prev"">&lt;</a>-"
		s_next = "=<a href=""{url}"" class=""prev"">&gt;</a>"
		s_prevg = "<a href=""{url}"" class=""prevg"">&lt;&lt;</a>--"
		s_nextg = "--<a href=""{url}"" class=""prevg"">&gt;&gt;</a>"
		s_nmod = "<a href=""{url}"" class=""now"">{page}</a>"
		s_amod = "<a href=""{url}"">{page}</a>"
		s_jump = "<input type=""text"" id=""{input}"" size=""2"" value=""{page}"" onkeydown=""{js}""/>"
		s_button = "<a href=""javascript:;"" onclick=""{js}"">GO</a>"
		s_select = "<select onchange=""{js}"">{option}</select>"
		s_forcedFirst = False
		s_forcedLast = False
		s_forcedPrev = False
		s_forcedNext = False
		s_inputId = "page"
		s_forcedPrevG = False
		s_forcedNextG = False
		s_paraStr = ""
		s_hrefModel = ""
		s_hiddenStr = ""
		s_identifier = "0000000000"
		s_model = "{first}" & vbcrlf & "{prev}" & vbcrlf & "{list}" & vbcrlf& "{next}" & vbcrlf & "{last}" & vbcrlf & "--{jump}" & vbcrlf & "--{button}" & vbcrlf & "--{select}"
	End Sub
	
	'''析构
	Private Sub Class_Terminate()
	End Sub
	
	'''设置跳转输入框ID,用于模型参数{button}
	'p_p:输入框ID名字符串
	Public Property Let InputId(Byval p_p)
		s_inputId =p_p
	End Property
	
	'''添加参数
	'p_n:参数名
	'p_v:参数值
	Public Sub AddPara(Byval p_n, Byval p_v)
		If Ucase(Method) = "POST" Then
			s_hiddenStr = s_hiddenStr & "<input type=""hidden"" name=""" & p_n & """ value=""" & p_v & """/>"
		Else
			s_paraStr = s_paraStr & "&" & p_n & "=" & p_v
		End If
	End Sub
	
	'''通用的模型设置接口
	'p_p:模型名称
	'p_v:模型值
	Public Sub [Set](Byval p_p, Byval p_v)
		If InStr("first,last,prev,next,nmod,amod,jump,button,select,prevg,nextg", p_p)>0 And InStr(p_p, ",")=0 Then
			p_v = Replace(p_v, """", """""")
			Execute("s_" & p_p & " = """ & p_v & """")
		End If
	End Sub
	
	'''获取相关模版是否强制显示
	'p_n:模型名
	Public Property Get Forced(Byval p_n)
		If InStr("first,last,prev,next,prevg,nextg", Lcase(p_n))>0 And InStr(p_n, ",")=0 Then
			Forced = Eval("s_forced" & p_n)
		Else
			Forced = False
		End If
	End Property
	
	'''设置相关模版是否强制显示
	'p_n:模型名
	'p_b:是否强制显示
	Public Property Let Forced(Byval p_n, Byval p_b)
		If InStr("first,last,prev,next", Lcase(p_n))>0 And InStr(p_n, ",")=0 Then
			Execute("s_forced" & p_n & " = " & p_b)
		End If
	End Property
	
	'''确认当前页码
	Private Sub makeSureNP_()
		If IsNumeric(PageNow)=False Then
			If Request(PageVar)="" Then
				PageNow = 1
			ElseIf Not(IsNumeric(Request(PageVar))) Then
				PageNow = 1
			ElseIf CLng(Trim(Request(PageVar)))<1 Then
				PageNow = 1
			Else
				PageNow = CLng(Trim(Request(PageVar)))
			End If
		Else
			If PageNow<=0 Then
				PageNow = 1
			End If
			PageNow = CLng(PageNow)
		End If
	End Sub
	
	'''对完整数据集进行分页处理,并获取到相关分页信息
	'p_o:完整未分页的数据集对象
	Public Sub FormatRs(Byval p_o)
		makeSureNP_()
		'强制转化一下PageSize类型
		PageSize = Clng(PageSize)
		If TypeName(p_o) = "Recordset" Then
			p_o.PageSize = PageSize
			PageCount = p_o.PageCount
			Recordcount = p_o.Recordcount
			If PageNow > PageCount Then
				PageNow = PageCount
			End If
			If Not p_o.Eof Then
				p_o.AbsolutePage = PageNow
			End If
		Else
			Response.Write(TypeName(p_o))
			Response.End
			Exit Sub
		End If
	End Sub
	
	'''设置页码识别符,必须是可处理数字,不能与既有参数相同,用于将静态正则规律化
	Public Property Let Identifier(Byval p_s)
		If IsNumeric(p_s) Then
			s_identifier = Cstr(p_s)
		End If
	End Property
	
	'''建立链接模型,如果含格式化函数,则在格式化函数中以{0}替代原链接
	Private Sub buildHrefModel_()
		''s_hrefModel = TargetPage & "?" & PageVar & "=" & s_identifier & s_paraStr
		s_hrefModel = TargetPage & "?" & Right(s_paraStr,Len(s_paraStr)-1) & "&" & PageVar & "=" & s_identifier
		If FormatUrlFun <> "" Then
			s_hrefModel = Eval(Replace(FormatUrlFun , "{0}", """" & s_hrefModel & """"))
		End If
	End Sub
	
	'''获取指定页的相应链接
	'p_i:指定页数
	Private Function hrefForPage_(Byval p_i)
		hrefForPage_ = Replace(s_hrefModel, s_identifier, p_i)
	End Function
	
	'''设置分页模版
	'p_s:模板字符串
	Public Property Let Model(Byval p_s)
		s_model = p_s
	End Property
	
	'''显示最终分页
	Public Property Get Out()
		makeSureNP_()
		If PageNow > PageCount Then
			PageNow = PageCount
		End If
		buildHrefModel_()
		'构建list
		Dim t_o, t_s, t_t, t_i, t_b, t_e, t_l, t_r
		Select Case ListMode
		Case 1
			'居中风格,首补齐,尾不补齐
			t_b = PageNow - LeftOffSet
			t_e = PageNow + RightOffSet
			If PageNow-LeftOffSet<1 Then
				t_b = 1
				t_e = LeftOffSet + RightOffSet + 1
			End If
		Case 2
			'居中风格,首补齐,尾补齐
			t_b = PageNow - LeftOffSet
			t_e = PageNow + RightOffSet
			If PageNow-LeftOffSet<1 Then
				t_b = 1
				t_e = LeftOffSet + RightOffSet + 1
			ElseIf PageNow+RightOffSet>PageCount Then
				t_b = PageCount - LeftOffSet - RightOffSet
				t_e = PageCount
			End If
		Case 3
			'固定风格,不适合大于100的分页
			t_t = LeftOffSet + RightOffSet
			t_t = Int(PageNow/t_t) * t_t
			t_b = t_t
			t_e = t_t + LeftOffSet + RightOffSet
		Case 4
			'魔法风格,自动添加缩减
			t_t = 2*Len(Cstr(PageNow))-4
			t_l = LeftOffSet - t_t
			t_r = RightOffSet - t_t
			t_b = PageNow - t_l
			t_e = PageNow + t_r
			If PageNow-t_l<1 Then
				t_b = 1
				t_e = LeftOffSet + RightOffSet + 1
			End If
		Case Else
			'固定风格,坚持按实际参数执行
			t_b = PageNow - LeftOffSet
			t_e = PageNow + RightOffSet
		End Select
		t_s = ""
		For t_i = t_b To t_e
			If t_i>0 And t_i<=PageCount Then
				If t_i = PageNow Then
					t_s = t_s & Replace(Replace(s_nmod, "{url}", hrefForPage_(t_i)), "{page}", t_i)
				Else
					t_s = t_s & Replace(Replace(s_amod, "{url}", hrefForPage_(t_i)), "{page}", t_i)
				End If
				If LineList And t_i<>t_e Then
					t_s = t_s & vbcrlf
				End If
			End If
		Next
		t_o = Replace(s_model, "{list}", t_s)
		'第一页
		If s_forcedFirst Or t_b>1 Then
			If InStr(s_model, "{first}")>0 Then
				t_o = Replace(t_o, "{first}", Replace(Replace(s_first, "{url}", hrefForPage_(1)), "{page}", 1))
			Else
				t_o = Replace(t_o, "{first}", "")
			End If
		Else
			t_o = Replace(t_o, "{first}", "")
		End If
		'最后页
		If s_forcedLast Or t_e<PageCount Then
			If InStr(s_model, "{last}")>0 Then
				t_o = Replace(t_o, "{last}", Replace(Replace(s_last, "{url}", hrefForPage_(PageCount)), "{page}", PageCount))
			Else
				t_o = Replace(t_o, "{last}", "")
			End If
		Else
			t_o = Replace(t_o, "{last}", "")
		End If
		'上一页
		If s_forcedPrev Or PageNow>1 Then
			If InStr(s_model, "{prev}")>0 Then
				t_o = Replace(t_o, "{prev}", Replace(Replace(s_prev, "{url}", hrefForPage_(PageNow-1)), "{page}", Cstr(PageNow-1)))
			Else
				t_o = Replace(t_o, "{prev}", "")
			End If
		Else
			t_o = Replace(t_o, "{prev}", "")
		End If
		'下一页
		If s_forcedNext Or PageNow<PageCount Then
			If InStr(s_model, "{next}")>0 Then
				t_o = Replace(t_o, "{next}", Replace(Replace(s_next, "{url}", hrefForPage_(PageNow+1)), "{page}", Cstr(PageNow+1)))
			Else
				t_o = Replace(t_o, "{next}", "")
			End If
		Else
			t_o = Replace(t_o, "{next}", "")
		End If
		'上一组
		If s_forcedPrevG Or PageNow-LeftOffSet>0 Then
			If InStr(s_model, "{prevg}")>0 Then
				t_o = Replace(t_o, "{prevg}", Replace(Replace(s_prevg, "{url}", hrefForPage_(PageNow-LeftOffSet)), "{page}", Cstr(PageNow-LeftOffSet)))
			Else
				t_o = Replace(t_o, "{prevg}", "")
			End If
		Else
			t_o = Replace(t_o, "{prevg}", "")
		End If
		'下一组
		If s_forcedNextG Or PageNow+RightOffSet<=PageCount Then
			If InStr(s_model, "{nextg}")>0 Then
				t_o = Replace(t_o, "{nextg}", Replace(Replace(s_nextg, "{url}", hrefForPage_(PageNow+RightOffSet)), "{page}", Cstr(PageNow+RightOffSet)))
			Else
				t_o = Replace(t_o, "{nextg}", "")
			End If
		Else
			t_o = Replace(t_o, "{nextg}", "")
		End If
		'跳转框
		If InStr(s_model, "{jump}")>0 Then
			t_s = "javascript:if(event.keyCode==13){var url='" & s_hrefModel & "';location=url.replace('" & s_identifier & "',this.value);return false;}"
			t_o = Replace(t_o, "{jump}", Replace(Replace(Replace(s_jump, "{input}", s_inputId), "{page}", PageNow), "{js}", t_s))
		End If
		'跳转按钮
		If InStr(s_model, "{button}")>0 Then
			t_s = "javascript:var url='" & s_hrefModel & "';location=url.replace('" & s_identifier & "',document.getElementById('" & s_inputId & "').value);return false;"
			t_o = Replace(t_o, "{button}", Replace(Replace(Replace(s_button, "{input}", s_inputId), "{page}", PageNow), "{js}", t_s))
		End If
		'下拉框
		If InStr(s_model, "{select}")>0 Then
			t_t = "javascript:var url='" & s_hrefModel & "';location=url.replace('" & s_identifier & "',this.value);return false;"
			t_s = Replace(s_select, "{js}", t_t)
			'处理option
			t_t = ""
			For t_i = 1 To PageCount
				If t_i = PageNow Then
					If OptionSelected Then
						t_t = t_t & "<option selected=""selected"" value=""" & t_i & """>" & OptionLeft & t_i & OptionRight & "</option>"
					Else
						t_t = t_t & "<option value=""" & t_i & """>" & OptionLeft & t_i & OptionRight & "</option>"
					End If
				Else
					t_t = t_t & "<option value=""" & t_i & """>" & OptionLeft & t_i & OptionRight & "</option>"
				End If
				If LineSelect Then
					t_t = t_t & vbcrlf
				End If
			Next
			t_s = Replace(t_s, "{option}", vbcrlf & t_t)
			t_o = Replace(t_o, "{select}", t_s)
		End If
		Out = t_o
	End Property
End Class
%>